package com.ruoyi.pingan.fymodule.order.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.pingan.fymodule.order.entity.SxoSafetyOrderSubsidy;
import com.ruoyi.pingan.fymodule.order.mapper.SxoSafetyOrderSubsidyMapper;
import com.ruoyi.pingan.fymodule.order.service.ISxoSafetyOrderService;
import com.ruoyi.pingan.fymodule.order.service.ISxoSafetyOrderSubsidyService;
import com.ruoyi.pingan.fymodule.thirdpa.model.dto.TraFinalStatusQueryDto;
import com.ruoyi.pingan.fymodule.thirdpa.model.vo.TraFinalStatusQueryVo;
import com.ruoyi.pingan.fymodule.thirdpa.service.PaOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 * 平安分账订单补贴 业务层实现
 *
 * @author wangy
 * @since 2025-06-23
 */
@Slf4j
@Service
public class SxoSafetyOrderSubsidyServiceImpl extends ServiceImpl<SxoSafetyOrderSubsidyMapper, SxoSafetyOrderSubsidy> implements ISxoSafetyOrderSubsidyService {

    @Autowired
    private PaOrderService paOrderService;
    @Autowired
    private ISxoSafetyOrderService sxoSafetyOrderService;


    @Override
    public void execute6273_6269() {
        log.info("补贴申请状态查询；时间:{}", DateUtil.now());
        Date date = new Date();
        Date startTime = DateUtil.offsetMinute(date, -180);
        Date endTime = DateUtil.offsetMinute(date, -120);
        LambdaQueryWrapper<SxoSafetyOrderSubsidy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SxoSafetyOrderSubsidy::getSubsidyStatus, 3);
        queryWrapper.eq(SxoSafetyOrderSubsidy::getStatus, 0);
        queryWrapper.ge(SxoSafetyOrderSubsidy::getSubsidyTime, DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"));
        queryWrapper.le(SxoSafetyOrderSubsidy::getSubsidyTime, DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss"));
        queryWrapper.orderByAsc(SxoSafetyOrderSubsidy::getId);
        List<SxoSafetyOrderSubsidy> lists = this.list(queryWrapper);
        if (lists.isEmpty()) {
            return;
        }
        for (SxoSafetyOrderSubsidy sxoSafetyOrderSubsidy : lists) {
            try {
                if (StrUtil.isEmpty(sxoSafetyOrderSubsidy.getJzbAcceptSeqNo())) {
                    continue;
                }
                //补贴成功且返回见证受理流水号
                TraFinalStatusQueryDto dto = new TraFinalStatusQueryDto();
                dto.setOldJzbAcceptSeqNo(sxoSafetyOrderSubsidy.getJzbAcceptSeqNo());
                TraFinalStatusQueryVo vo = paOrderService.traFinalStatusQuery(dto);
                //执行vo的存储
                //交易状态0-成功；1-失败；3-处理中
                if (!"6269".equals(vo.getTranType())) {
                    continue;
                }
                SxoSafetyOrderSubsidy mod = new SxoSafetyOrderSubsidy();
                mod.setId(sxoSafetyOrderSubsidy.getId());
                String tranDateStr = vo.getTranDate().concat(vo.getTranTime());
                Date tranDate = DateUtil.parse(tranDateStr);
                String succ = DateUtil.format(tranDate, "yyyy-MM-dd HH:mm:ss");
                if ("0".equals(vo.getTranStatus())) {
                    //0：未补贴；1：已补贴；2撤销补贴
                    mod.setSubsidyStatus(1);
                    //补贴时间
                    mod.setSubsidySuccTime(succ);
                } else {
                    mod.setSubsidyStatus(-1);
                    mod.setUpdTime(DateUtil.now());
                    mod.setErrorMsg(vo.getTranDealStatusDesc());
                }
                this.updateById(mod);


               /*SxoSafetyOrder modO = sxoSafetyOrderService.getById(sxoSafetyOrderSubsidy.getSafetyOrderId());
                if (modO != null) {
                    SxoSafetyOrder modU = new SxoSafetyOrder();
                    modU.setId(modO.getId());
                    if ("0".equals(vo.getTranStatus())) {
                        modU.setSubsidyStatus(1);
                        //modU.setSubsidySuccTime(succ);
                    } else {
                        modU.setSubsidyStatus(-2);
                    }
                    sxoSafetyOrderService.updateById(modU);
                }*/

            } catch (Exception e) {
                log.error("execute6273_6269执行失败：{}", e.getMessage());
            }

        }
        log.info("补贴申请状态查询；时间:{}；数量：{}", DateUtil.now(), lists.size());
    }

    @Override
    public void execute6273_6270() {

        List<SxoSafetyOrderSubsidy> lists = this.list();
        if (lists.size() > 0) {
            for (SxoSafetyOrderSubsidy sxoSafetyOrderSubsidy : lists) {
                //补贴撤销成功且返回见证受理流水号
                if (sxoSafetyOrderSubsidy.getJzbAcceptSeqNo() != null && sxoSafetyOrderSubsidy.getSubsidyStatus() == 1) {
                    TraFinalStatusQueryDto dto = new TraFinalStatusQueryDto();
                    dto.setOldJzbAcceptSeqNo(sxoSafetyOrderSubsidy.getJzbAcceptSeqNo());
                    TraFinalStatusQueryVo vo = paOrderService.traFinalStatusQuery(dto);
                    //执行vo的存储
                    //交易状态0-成功；1-失败；3-处理中
                    if ("0".equals(vo.getTranStatus()) && "6270".equals(vo.getTranType())) {

                        //0：未补贴；1：已补贴；2撤销补贴
                        sxoSafetyOrderSubsidy.setSubsidyStatus(2);
                        //补贴撤销时间
                        sxoSafetyOrderSubsidy.setSubsidyTime(vo.getTranDate());
                        //补贴金额
                        sxoSafetyOrderSubsidy.setSubsidyAmt(vo.getTranTotalAmt());
                        this.updateById(sxoSafetyOrderSubsidy);
                    }

                }

            }

        }


    }

}